from fastapi import FastAPI
from typing import List,Optional
import uvicorn


app = FastAPI( title = "菜单管理")


menu_items = []#这是空的哦

@app.post("/order")
def creat_menu(id : int, name : str, price : float, description : Optional[str] = None):
    new_menu = {
        "id" : id, 
        "name" : name, 
        "price" : price,
        "description" : description
    }
    menu_items.append(new_menu) 
    return {"message" : "菜单创建成功u","menu" : new_menu }#加入我的菜单

@app.get("/order")#查all
def get_all_menus():
    return {"menu_items" : menu_items}

@app.get("/order/{menu_id}")#依旧查，但查单个
def query_menu(menu_id : int):
    for item in menu_items:
        if item['id'] == menu_id:
            return item
        return {"message" : "本店没有上新这道菜哦"}
 
@app.put("/order")
def update_menu(menu_id : int, name : Optional[str] = None,
                price : Optional[float] = None,
                description : Optional[str] = None):
    for item in menu_items:
       if item["id"] == menu_id:
           if name:
               item["name"] == name
           if price:
               item["price"] == price
           if description:
               item["description"] == description
           return {"message" : "你成功添加了一个菜单哟", "menu": item}
       return {"mssage" : "本店真的没有上新这道菜哦"}
    
@app.delete("/order/{menu_id}")
def delete_menu(menu_id: int):
    global menu_items
    menu_items = [item for item in menu_items if item["id"] != menu_id]
    if not menu_items or (len(menu_items) < len(menu_items) + 1):
        return {"message" : "你删掉了一个菜单"}
    return {"message" : "你并没有找到你想要的菜单"}